package cn.xfdzx.android.apps.shop.activity.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.luck.picture.lib.tools.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.activity.UserAgreementActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.LogininfoBean;
import cn.xfdzx.android.apps.shop.contract.PassWordLoginContract;
import cn.xfdzx.android.apps.shop.evenbus.IMloginMessage;
import cn.xfdzx.android.apps.shop.presenter.PassWordLoginPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.RegexUtil;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/*
/* 账号、密码登录页面
 */
public class PasswordLoginActivity extends BaseMvpActivity<PassWordLoginPresenter> implements PassWordLoginContract.View , TextWatcher {

    @BindView(R.id.iv_back)
    ImageView ivBack;      //标题栏-左边按钮

    @BindView(R.id.password_phone)
    EditText passwordPhone;    //手机号输入框
    @BindView(R.id.password_password)
    EditText passwordPassword;  //密码输入框
    @BindView(R.id.password_info)
    TextView passwordInfo;    //红色字体提示文案
    @BindView(R.id.password_agreement)
    TextView passwordAgreement;  //用户协议
    @BindView(R.id.password_privacy)
    TextView passwordPrivacy;   //隐私权限
    @BindView(R.id.password_login)
    TextView passwordLogin;   //登录
    @BindView(R.id.password_forget)
    TextView passwordForget; //忘记密码
    @BindView(R.id.tv_password_wx_login)
    ImageView passwordWxLogin;   //微信登录

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_passwordlogin;
    }

    @Override
    public void initView() {

        mPresenter=new PassWordLoginPresenter();
        mPresenter.attachView(this);

        passwordPhone.addTextChangedListener(this);
        passwordPassword.addTextChangedListener(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onPassWordLoginSuccess(LogininfoBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            APP_TOKEN=bean.getData().getToken();

            UtilsData.getPreference(this,"login").put("token", APP_TOKEN);
            UtilsData.getPreference(this,"login").put("is_store", EmptyUtil.isEmpty(bean.getData().getUserInfo().getIs_store())?0:bean.getData().getUserInfo().getIs_store());
            UtilsData.getPreference(this,"login").put("nickname", EmptyUtil.isEmpty(bean.getData().getUserInfo().getNickname())?"":bean.getData().getUserInfo().getNickname());
            UtilsData.getPreference(this,"login").put("head_url", EmptyUtil.isEmpty(bean.getData().getUserInfo().getAvatar())?"":bean.getData().getUserInfo().getAvatar());
            EventBus.getDefault().post(new IMloginMessage(1));//通知im登录

            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.iv_back, R.id.password_agreement, R.id.password_privacy, R.id.password_login, R.id.password_forget, R.id.tv_password_wx_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.password_agreement:   //用户协议点击
                Intent intentuser = new Intent(this, UserAgreementActivity.class);
                intentuser.putExtra("type", "0");
                startActivity(intentuser);
                break;
            case R.id.password_privacy:    //隐私权限点击
                Intent intentprivacy = new Intent(this, UserAgreementActivity.class);
                intentprivacy.putExtra("type", "1");
                startActivity(intentprivacy);
                break;
            case R.id.password_login:   //登录点击
                if (EmptyUtil.isEmpty(passwordPhone.getText().toString())) {
                    ToastUtils.s(PasswordLoginActivity.this, "请输入手机号码");
                    return;
                }
                if (EmptyUtil.isEmpty(passwordPassword.getText().toString())) {
                    ToastUtils.s(PasswordLoginActivity.this, "请输入密码");
                    return;
                }
                mPresenter.passWordLogin(passwordPhone.getText().toString(), passwordPassword.getText().toString());
                break;
            case R.id.password_forget:  //忘记密码点击
                startActivity(new Intent(this, ForgetPasswordActivity.class));
                break;
            case R.id.tv_password_wx_login:  //微信登录点击
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (EmptyUtil.isEmpty(passwordPhone.getText().toString()) ||
                EmptyUtil.isEmpty(passwordPassword.getText().toString())) {

            //mobileLogin.setClickable(false);
            passwordLogin.setBackground(
                    ContextCompat.getDrawable(this, R.drawable.password_login_button));
        } else {
            // mobileLogin.setClickable(true);
            passwordLogin.setBackground(
                    ContextCompat.getDrawable(this, R.drawable.oneclick_button));
        }
    }
}
